import { $alert as globalAlert } from "@/components/mars-ui/index"
import {
  Home,
  Local,
  PreviewOpen,
  AppSwitch,
  Forbid,
  Cube,
  MultiTriangular,
  Shovel,
  Close,
  MapDistance,
  Ruler,
  Texture,
  AutoHeightOne,
  Compass,
  DeleteKey,
  Mark,
  Label,
  Change,
  BringToFrontOne,
  Asterisk,
  Rectangle,
  Editor,
  Export,
  ClearFormat,
  Effects,
  LightRain,
  Snow,
  Fog,
  Halo,
  Brightness,
  DarkMode,
  Blackboard,
  HighLight,
  Config,
  LandSurveying,
  TwoTriangles,
  Sun,
  FlightAirflow,
  AddPicture,
  SwitchThemes,
  Agreement,
  TakeOff,
  KeyboardOne,
  RecentViewsSort,
  MoveInOne,
  ExclusiveGateway,
  CloseOne,
  LockOne,
  Box,
  MonitorOff,
  MapTwo,
  International,
  DatabaseForbid
} from "@icon-park/svg"
import { clearLayers, layersHasOverlay } from "@/common/uses/use-layer"

// 获取平台内置的右键菜单
export function getDefaultContextMenu(DE, viewer) {
  const Cesium = DE.__namespace.Cesium
  // 绕点飞行
  const contextMenu = viewer.contextMenu
  const keyboardRoaming = new DE.KeyboardRoaming(viewer)
  const measure = new DE.Measure(viewer)
  const weather = new DE.Weather(viewer)
  const plot = new DE.Plot(viewer)
  const effect = new DE.Effect(viewer)
  return [
    {
      // eslint-disable-next-line quotes
      label: "初始视图",
      icon: Home({ theme: "outline", fill: "#fff", size: "18" }),
      callback: function (e) {
        viewer && viewer.flyHome()
      }
    },
    {
      // eslint-disable-next-line quotes
      label: "查看此处坐标",
      icon: Local({ theme: "outline", fill: "#fff", size: "18" }),
      show: function (e) {
        return Cesium.defined(e.surfacePosition)
      },
      callback: function (e) {
        // 经纬度
        const mpt = e.wgs84SurfacePosition
        const inhtml = `
         经度:${mpt.lng.toFixed(6)}, 纬度:${mpt.lat.toFixed(6)}, 海拔:${mpt.alt.toFixed(6)},
         横坐标:${e.surfacePosition.x.toFixed(1)}, 纵坐标:${e.surfacePosition.y.toFixed(1)}
        `
        globalAlert(inhtml, "位置信息")
      }
    },
    {
      label: "查看当前视角",
      icon: PreviewOpen({ theme: "outline", fill: "#fff", size: "18" }),
      callback: function (e) {
        const mpt = JSON.stringify(viewer.cameraPosition)
        globalAlert(mpt, "当前视角信息")
      }
    },
    {
      label: "图层清空",
      icon: ClearFormat({ theme: "outline", fill: "#fff", size: "18" }),
      show: function (e) {
        return layersHasOverlay()
      },
      callback: function (e) {
        clearLayers()
      }
    },
    {
      label: "视角切换",
      icon: SwitchThemes({ theme: "outline", fill: "#fff", size: "18" }),
      show: function (e) {
        return Cesium.defined(e.surfacePosition)
      },
      children: [
        {
          label: "绕此处环绕飞行",
          icon: TakeOff({ theme: "outline", fill: "#fff", size: "18" }),
          show: function (e) {
            return e.wgs84SurfacePosition && (!contextMenu.aroundView || !contextMenu.aroundView.isStart)
          },
          callback: function (e) {
            const mpt = e.wgs84SurfacePosition
            const alt = viewer.cameraPosition ? viewer.cameraPosition.alt : 500
            const heading = viewer.cameraPosition ? viewer.cameraPosition.heading : 0
            viewer.flyToPosition(new DE.Position(mpt.lng, mpt.lat, alt, heading, -90), () => {
              contextMenu.aroundView.start()
            })
          }
        },
        {
          label: "关闭环绕飞行",
          icon: CloseOne({ theme: "outline", fill: "#fff", size: "18" }),
          show: function (e) {
            return contextMenu.aroundView?.isStart
          },
          callback: function (e) {
            if (contextMenu.aroundView) {
              contextMenu.aroundView.stop()
            }
          }
        },
        {
          label: "移动到此处",
          icon: MoveInOne({ theme: "outline", fill: "#fff", size: "18" }),
          show: function (e) {
            return Cesium.defined(e.surfacePosition)
          },
          callback: function (e) {
            const mpt = e.wgs84SurfacePosition
            mpt.alt = 500
            mpt.heading = 0
            mpt.pitch = -90
            viewer.flyToPosition(
              mpt,
              () => {
                console.log("飞行完成!")
              },
              2
            )
          }
        },
        {
          label: "第一视角站到此处",
          icon: RecentViewsSort({ theme: "outline", fill: "#fff", size: "18" }),
          show: function (e) {
            return Cesium.defined(e.wgs84SurfacePosition)
          },
          callback: function (e) {
            const mpt = e.wgs84SurfacePosition
            mpt.alt = 500
            mpt.heading = viewer?.cameraPosition?.heading
            mpt.pitch = 0.0
            mpt.roll = 0.0
            viewer.flyToPosition(
              mpt,
              () => {
                console.log("第一视角站立完成!")
              },
              2
            )
          }
        },
        {
          label: "开启键盘漫游",
          icon: KeyboardOne({ theme: "outline", fill: "#fff", size: "18" }),
          show: function (e) {
            return !keyboardRoaming.enable
          },
          callback: function (e) {
            keyboardRoaming.enable = true
          }
        },
        {
          label: "关闭键盘漫游",
          icon: CloseOne({ theme: "outline", fill: "#fff", size: "18" }),
          show: function (e) {
            return keyboardRoaming.enable
          },
          callback: function (e) {
            keyboardRoaming.enable = false
          }
        },
        {
          label: "取消锁定",
          icon: LockOne({ theme: "outline", fill: "#fff", size: "18" }),
          show: function (e) {
            // viewer.delegate 即为 Cesium.Viewer对象
            return viewer.delegate.trackedEntity !== undefined
          },
          callback: function (e) {
            viewer.delegate.trackedEntity = undefined
          }
        }
      ]
    },
    {
      label: "图上量算",
      show: function (e) {
        return Cesium.defined(e.surfacePosition)
      },
      icon: Ruler({ theme: "outline", fill: "#fff", size: "18" }),
      children: [
        {
          label: "空间测距",
          icon: MapDistance({ theme: "outline", fill: "#fff", size: "18" }),
          callback: function (e) {
            measure.distance()
          }
        },
        {
          label: "贴地测距",
          icon: MapDistance({ theme: "outline", fill: "#fff", size: "18" }),
          callback: function (e) {
            measure.distanceSurface()
          }
        },
        {
          label: "空间测面",
          icon: Texture({ theme: "outline", fill: "#fff", size: "18" }),
          callback: function (e) {
            measure.area()
          }
        },
        {
          label: "贴地测面",
          icon: Texture({ theme: "outline", fill: "#fff", size: "18" }),
          callback: function (e) {
            measure.areaSurface()
          }
        },
        {
          label: "高度差",
          icon: AutoHeightOne({ theme: "outline", fill: "#fff", size: "18" }),
          callback: function (e) {
            measure.height()
          }
        },
        {
          label: "贴物高度差",
          icon: AutoHeightOne({ theme: "outline", fill: "#fff", size: "18" }),
          callback: function (e) {
            measure.height({
              clampToModel: true
            })
          }
        },
        {
          label: "角度",
          icon: Compass({ theme: "outline", fill: "#fff", size: "18" }),
          callback: function (e) {
            measure.angle()
          }
        },
        {
          label: "贴物角度",
          icon: Compass({ theme: "outline", fill: "#fff", size: "18" }),
          callback: function (e) {
            measure.angle({
              clampToModel: true
            })
          }
        },
        {
          label: "偏航角",
          icon: Compass({ theme: "outline", fill: "#fff", size: "18" }),
          callback: function (e) {
            measure.heading()
          }
        },
        {
          label: "三角测量",
          icon: Compass({ theme: "outline", fill: "#fff", size: "18" }),
          callback: function (e) {
            measure.triangleHeight()
          }
        },
        {
          label: "删除测量",
          icon: DeleteKey({ theme: "outline", fill: "#fff", size: "18" }),
          show: function (e) {
            return measure && measure.hasMeasure
          },
          callback: function (e) {
            if (measure) {
              measure.deactivate()
            }
          }
        }
      ]
    },
    {
      label: "图上标记",
      show: function (e) {
        return Cesium.defined(e.surfacePosition)
      },
      icon: Mark({ theme: "outline", fill: "#fff", size: "18" }),
      children: [
        {
          label: "标记点",
          icon: Label({ theme: "outline", fill: "#fff", size: "18" }),
          callback: function (e) {
            plot &&
              plot.draw("point", (overlay) => {
                if (overlay) {
                  viewer.plotLayer.addOverlay(overlay)
                  overlay.contextMenu = [
                    {
                      label: "编辑点",
                      callback: (e) => {
                        plot.edit(overlay)
                      }
                    }
                  ]
                  overlay.defaultMenuShow = false
                }
              })
          }
        },
        {
          label: "标记线",
          icon: Change({ theme: "outline", fill: "#fff", size: "18" }),
          callback: function (e) {
            plot &&
              plot.draw("polyline", (overlay) => {
                if (overlay) {
                  viewer.plotLayer.addOverlay(overlay)
                  overlay.contextMenu = [
                    {
                      label: "编辑线",
                      callback: (e) => {
                        plot.edit(overlay)
                      }
                    }
                  ]
                }
              })
          }
        },
        {
          label: "标记面",
          icon: BringToFrontOne({ theme: "outline", fill: "#fff", size: "18" }),
          callback: function (e) {
            plot &&
              plot.draw("polygon", (overlay) => {
                if (overlay) {
                  viewer.plotLayer.addOverlay(overlay)
                  overlay.contextMenu = [
                    {
                      label: "编辑面",
                      callback: (e) => {
                        plot.edit(overlay)
                      }
                    }
                  ]
                }
              })
          }
        },
        {
          label: "标记圆",
          icon: Asterisk({ theme: "outline", fill: "#fff", size: "18" }),
          callback: function (e) {
            plot &&
              plot.draw("circle", (overlay) => {
                if (overlay) {
                  console.log(viewer.plotLayer)
                  viewer.plotLayer.addOverlay(overlay)
                  overlay.contextMenu = [
                    {
                      label: "编辑圆",
                      callback: (e) => {
                        plot.edit(overlay)
                      }
                    }
                  ]
                }
              })
          }
        },
        {
          label: "标记矩形",
          icon: Rectangle({ theme: "outline", fill: "#fff", size: "18" }),
          callback: function (e) {
            plot &&
              plot.draw("rect", (overlay) => {
                if (overlay) {
                  viewer.plotLayer.addOverlay(overlay)
                  overlay.contextMenu = [
                    {
                      label: "编辑矩形",
                      callback: (e) => {
                        plot.edit(overlay)
                      }
                    }
                  ]
                }
              })
          }
        },
        {
          label: "清除所有标记",
          icon: ClearFormat({ theme: "outline", fill: "#fff", size: "18" }),
          show: function (e) {
            return viewer.plotLayer.entities.values.length > 0
          },
          callback: function (e) {
            viewer.plotLayer.clear()
          }
        }
      ]
    },
    {
      label: "特效效果",
      icon: Effects({ theme: "outline", fill: "#fff", size: "18" }),
      children: [
        {
          label: "开启下雨",
          icon: LightRain({ theme: "outline", fill: "#fff", size: "18" }),
          show: function (e) {
            return !weather.rain.enable
          },
          callback: function (e) {
            if (!weather.rain.enable) {
              weather.rain.enable = true
            }
          }
        },
        {
          label: "关闭下雨",
          icon: Close({ theme: "outline", fill: "#fff", size: "18" }),
          show: function (e) {
            return weather.rain.enable
          },
          callback: function (e) {
            if (weather.rain.enable) {
              weather.rain.enable = false
            }
          }
        },
        {
          label: "开启下雪",
          icon: Snow({ theme: "outline", fill: "#fff", size: "18" }),
          show: function (e) {
            return !weather.snow.enable
          },
          callback: function (e) {
            if (!weather.snow.enable) {
              weather.snow.enable = true
            }
          }
        },
        {
          label: "关闭下雪",
          icon: CloseOne({ theme: "outline", fill: "#fff", size: "18" }),
          show: function (e) {
            return weather.snow.enable
          },
          callback: function (e) {
            if (weather.snow.enable) {
              weather.snow.enable = false
            }
          }
        },

        {
          label: "开启雾天气",
          icon: Fog({ theme: "outline", fill: "#fff", size: "18" }),
          show: function (e) {
            return !weather.fog.enable
          },
          callback: function (e) {
            if (!weather.fog.enable) {
              weather.fog.enable = true
              weather.fog.color = DE.Color.fromCssColorString("#f5f4f4")
              weather.fog.fogByDistance = {
                near: 20,
                far: 200000
              }
            }
          }
        },
        {
          label: "关闭雾天气",
          icon: CloseOne({ theme: "outline", fill: "#fff", size: "18" }),
          show: function (e) {
            return weather.fog.enable
          },
          callback: function (e) {
            if (weather.fog.enable) {
              weather.fog.enable = false
            }
          }
        },
        {
          label: "开启泛光",
          icon: Halo({ theme: "outline", fill: "#fff", size: "18" }),
          show: function (e) {
            return !effect.bloom.enable
          },
          callback: function (e) {
            if (!effect.bloom.enable) {
              effect.bloom.enable = true
              effect.bloom.contrast = 128
              effect.bloom.brightness = -0.3
              effect.bloom.delta = 1
              effect.bloom.stepSize = 3.8
              effect.bloom.sigma = 5
            }
          }
        },
        {
          label: "关闭泛光",
          icon: CloseOne({ theme: "outline", fill: "#fff", size: "18" }),
          show: function (e) {
            return effect.bloom.enable
          },
          callback: function (e) {
            if (effect.bloom.enable) {
              effect.bloom.enable = false
            }
          }
        },

        {
          label: "开启亮度",
          icon: Brightness({ theme: "outline", fill: "#fff", size: "18" }),
          show: function (e) {
            return !effect.brightness.enable
          },
          callback: function (e) {
            if (!effect.brightness.enable) {
              effect.brightness.enable = true
              effect.brightness.intensity = 1.5
            }
          }
        },
        {
          label: "关闭亮度",
          icon: CloseOne({ theme: "outline", fill: "#fff", size: "18" }),
          show: function (e) {
            return effect.brightness.enable
          },
          callback: function (e) {
            if (effect.brightness.enable) {
              effect.brightness.enable = false
            }
          }
        },

        {
          label: "开启夜视",
          icon: DarkMode({ theme: "outline", fill: "#fff", size: "18" }),
          show: function (e) {
            return !effect.night.enable
          },
          callback: function (e) {
            if (!effect.night.enable) {
              effect.night.enable = true
            }
          }
        },
        {
          label: "关闭夜视",
          icon: CloseOne({ theme: "outline", fill: "#fff", size: "18" }),
          show: function (e) {
            return effect.night.enable
          },
          callback: function (e) {
            if (effect.night.enable) {
              effect.night.enable = false
            }
          }
        },
        {
          label: "开启黑白",
          icon: Blackboard({ theme: "outline", fill: "#fff", size: "18" }),
          show: function (e) {
            return !effect.blackAndWhite.enable
          },
          callback: function (e) {
            if (!effect.blackAndWhite.enable) {
              effect.blackAndWhite.enable = true
              effect.blackAndWhite.gradations = 4
            }
          }
        },
        {
          label: "关闭黑白",
          icon: CloseOne({ theme: "outline", fill: "#fff", size: "18" }),
          show: function (e) {
            return effect.blackAndWhite.enable
          },
          callback: function (e) {
            if (effect.blackAndWhite.enable) {
              effect.blackAndWhite.enable = false
            }
          }
        },
        {
          label: "开启轮廓",
          icon: HighLight({ theme: "outline", fill: "#fff", size: "18" }),
          show: function (e) {
            return !effect.silhouette.enable
          },
          callback: function (e) {
            if (!effect.silhouette.enable) {
              effect.silhouette.enable = true
            }
          }
        },
        {
          label: "关闭轮廓",
          icon: CloseOne({ theme: "outline", fill: "#fff", size: "18" }),
          show: function (e) {
            return effect.silhouette.enable
          },
          callback: function (e) {
            if (effect.silhouette.enable) {
              effect.silhouette.enable = false
            }
          }
        }
      ]
    },
    {
      label: "场景设置",
      icon: Config({ theme: "outline", fill: "#fff", size: "18" }),
      children: [
        {
          label: "开启深度监测",
          icon: LandSurveying({ theme: "outline", fill: "#fff", size: "18" }),
          show: function (e) {
            const viewerOptions = viewer && viewer.getOptions()
            return viewerOptions?.cameraController?.enableCollisionDetection
          },
          callback: function (e) {
            viewer &&
              viewer.setOptions({
                cameraController: {
                  enableCollisionDetection: false // 是否支持碰撞检测
                }
              })
          }
        },
        {
          label: "关闭深度监测",
          icon: CloseOne({ theme: "outline", fill: "#fff", size: "18" }),
          show: function (e) {
            const viewerOptions = viewer && viewer.getOptions()
            return !viewerOptions?.cameraController?.enableCollisionDetection
          },
          callback: function (e) {
            viewer &&
              viewer.setOptions({
                cameraController: {
                  enableCollisionDetection: true // 是否支持碰撞检测
                }
              })
          }
        },
        {
          label: "显示星空背景",
          icon: TwoTriangles({ theme: "outline", fill: "#fff", size: "18" }),
          show: function (e) {
            return !viewer.getOptions().skyBox.show
          },
          callback: function (e) {
            viewer.setOptions({
              skyBox: {
                show: true // 天空盒
              },
              moon: {
                show: true // 月亮
              },
              sun: {
                show: true // 太阳
              }
            })
          }
        },
        {
          label: "关闭星空背景",
          icon: ExclusiveGateway({ theme: "outline", fill: "#fff", size: "18" }),
          show: function (e) {
            return viewer.getOptions().skyBox.show
          },
          callback: function (e) {
            viewer.setOptions({
              skyBox: {
                show: false // 天空盒
              },
              moon: {
                show: false // 月亮
              },
              sun: {
                show: false // 太阳
              }
            })
          }
        },
        {
          label: "开启日照阴影",
          icon: Sun({ theme: "outline", fill: "#fff", size: "18" }),
          show: function (e) {
            return !viewer.getOptions().globe.enableLighting
          },
          callback: function (e) {
            viewer.setOptions({
              globe: {
                enableLighting: true
              }
            })
          }
        },
        {
          label: "关闭日照阴影",
          icon: CloseOne({ theme: "outline", fill: "#fff", size: "18" }),
          show: function (e) {
            return viewer.getOptions().globe.enableLighting
          },
          callback: function (e) {
            viewer.setOptions({
              globe: {
                enableLighting: false
              }
            })
          }
        },
        {
          label: "开启大气渲染",
          icon: FlightAirflow({ theme: "outline", fill: "#fff", size: "18" }),
          show: function (e) {
            return !viewer.getOptions().globe.showGroundAtmosphere
          },
          callback: function (e) {
            viewer.setOptions({
              globe: {
                showGroundAtmosphere: true
              }
            })
          }
        },
        {
          label: "关闭大气渲染",
          icon: CloseOne({ theme: "outline", fill: "#fff", size: "18" }),
          show: function (e) {
            return viewer.getOptions().globe.showGroundAtmosphere
          },
          callback: function (e) {
            viewer.setOptions({
              globe: {
                showGroundAtmosphere: false
              }
            })
          }
        },
        {
          label: "场景出图",
          icon: AddPicture({ theme: "outline", fill: "#fff", size: "18" }),
          callback: function (e) {
            viewer.exportScene("场景出图")
          }
        }
      ]
    }

    // {
    //   label: "地形服务",
    //   icon: International({ theme: "outline", fill: "#fff", size: "18" }),
    //   show: function (e) {
    //     return Cesium.defined(e.cartesian)
    //   },
    //   children: [
    //     {
    //       label: "开启地形",
    //       icon: MapTwo({ theme: "outline", fill: "#fff", size: "18" }),
    //       show: function (e) {
    //         return !map.hasTerrain
    //       },
    //       callback: function (e) {
    //         map.hasTerrain = true
    //       }
    //     },
    //     {
    //       label: "关闭地形",
    //       icon: CloseOne({ theme: "outline", fill: "#fff", size: "18" }),
    //       show: function (e) {
    //         return map.hasTerrain
    //       },
    //       callback: function (e) {
    //         map.hasTerrain = false
    //       }
    //     },
    //     {
    //       label: "显示三角网",
    //       icon: MultiTriangular({ theme: "outline", fill: "#fff", size: "18" }),
    //       show: function (e) {
    //         return !map.scene.globe._surface.tileProvider._debug.wireframe
    //       },
    //       callback: function (e) {
    //         map.scene.globe._surface.tileProvider._debug.wireframe = true
    //       }
    //     },
    //     {
    //       label: "关闭三角网",
    //       icon: CloseOne({ theme: "outline", fill: "#fff", size: "18" }),
    //       show: function (e) {
    //         return map.scene.globe._surface.tileProvider._debug.wireframe
    //       },
    //       callback: function (e) {
    //         map.scene.globe._surface.tileProvider._debug.wireframe = false
    //       }
    //     }
    //   ]
    // },
  ]
}
